/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.justina.inaction;
import java.io.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.*;

/**
 *
 * @author Justina
 */
public class Searcher {

    public static void search(String indexDir, String q) throws IOException, ParseException {
        Directory dir = FSDirectory.open(new File(indexDir));
        IndexReader ir = IndexReader.open(dir);
        IndexSearcher is = new IndexSearcher(ir);
        
        QueryParser parser = new QueryParser(Version.LUCENE_30, "filename",
                new StandardAnalyzer(Version.LUCENE_30));
        Query query = parser.parse(q);
        long start = System.currentTimeMillis();
        TopDocs hits = is.search(query, 10);
        long end = System.currentTimeMillis();
        
        
        
        System.err.println("Found " + hits.totalHits + " document(s) (in "
                + (end - start) + " milliseconds) that matched query '" 
                + q + " in " + is.maxDoc());
        
        for (ScoreDoc scoreDoc: hits.scoreDocs) {
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("fullpath"));
        }
        is.close();
    }
    
}
